package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import model.Model;
import sim.Simulator;

@SuppressWarnings("serial")
public class MainMenuView extends CtfPanel {
	private JPanel startServerButton;
	private JPanel startClientButton;
	Image image;
	
	public MainMenuView(CtfFrame window) {
		super(window);
		this.setBackground(Color.black);
		this.setLayout(null);
		startServerButton = new StartServerButton();
		this.add(startServerButton);
		startClientButton = new StartClientButton();
		this.add(startClientButton);
		try {
			this.image = javax.imageio.ImageIO.read(new java.net.URL(getClass()
					.getResource("/img/lotties.jpg"), "lotties.jpg"));
		} catch (Exception e) { /* handled in paintComponent() */
		}
		this.repaint();
	}
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		startServerButton.setBounds(0, (int)(getHeight()*0.85), (int)(getWidth()*0.5), (int)(getHeight()*0.15));
		startClientButton.setBounds((int)(getWidth()*0.5), (int)(getHeight()*0.85), (int)(getWidth()*0.5), (int)(getHeight()*0.15));
		if (this.image != null)
			g.drawImage(image, 0, 0, this.getWidth(), (int)(0.85*this.getHeight()), this);
	}

	public class StartServerButton extends JPanel implements MouseListener {
		private Image normalImage;
		private Image hoverImage;
		private Image currentImage;
		public StartServerButton() {
			this.addMouseListener(this);
			try {
				this.normalImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/startserver.png"), "startserver.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			try {
				this.hoverImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/startserver_hover.png"), "startserver_hover.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			currentImage = normalImage;
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			this.currentImage = this.hoverImage;
			this.repaint();
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
			this.currentImage = this.normalImage;
			this.repaint();
		}
		@Override
		public void mousePressed(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseReleased(MouseEvent arg0) {
			setView(new SetupServerView(getWindow()));
		}
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (this.currentImage != null)
				g.drawImage(this.currentImage, 0, 0, this.getWidth(), this.getHeight(), this);
		}

	}
	public class StartClientButton extends JPanel implements MouseListener {
		private Image normalImage;
		private Image hoverImage;
		private Image currentImage;
		public StartClientButton() {
			this.addMouseListener(this);
			try {
				this.normalImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/startclient.png"), "startclient.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			try {
				this.hoverImage = javax.imageio.ImageIO.read(new java.net.URL(getClass()
						.getResource("/img/startclient_hover.png"), "startclient_hover.png"));
			} catch (Exception e) { /* handled in paintComponent() */
			}
			currentImage = normalImage;
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
			this.currentImage = this.hoverImage;
			this.repaint();
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
			this.currentImage = this.normalImage;
			this.repaint();
		}
		@Override
		public void mousePressed(MouseEvent arg0) {
			//nothing
		}
		@Override
		public void mouseReleased(MouseEvent arg0) {
			Model model = new Model();
			GameView view = new GameView(getWindow(), model);
			@SuppressWarnings("unused")
			Simulator sim = new Simulator(model);
			setView(view);
		}
		@Override
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (this.currentImage != null)
				g.drawImage(this.currentImage, 0, 0, this.getWidth(), this.getHeight(), this);
		}

	}
}
